package com.qnbc.mystery.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qnbc.mystery.core.annotation.Log;
import com.qnbc.mystery.core.controller.BaseController;
import com.qnbc.mystery.core.domain.AjaxResult;
import com.qnbc.mystery.core.enums.BusinessType;
import com.qnbc.mystery.core.page.TableDataInfo;
import com.qnbc.mystery.core.utils.poi.ExcelUtil;
import com.qnbc.mystery.pojo.domain.FoodsProduct;
import com.qnbc.mystery.pojo.domain.User;
import com.qnbc.mystery.pojo.param.BoxAddOrderParam;
import com.qnbc.mystery.pojo.param.BoxProductParam;
import com.qnbc.mystery.service.IProductService;
import com.qnbc.mystery.utils.TokenUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qnbc.mystery.domain.BoxProduct;
import com.qnbc.mystery.service.IBoxProductService;

/**
 * 奖盒关联商品
 *
 * @author guzj
 * @date 2024-01-30
 */
@Api("奖盒关联商品管理")
@RestController
@RequestMapping("/mystery/boxProduct")
public class BoxProductController extends BaseController {
    @Autowired
    private IBoxProductService boxProductService;

    @Autowired
    private IProductService productService;

    /**
     * 查询奖盒关联商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BoxProduct boxProduct) {
        startPage();
        List<BoxProduct> list = boxProductService.selectBoxProductList(boxProduct);
        for (BoxProduct product : list) {
            FoodsProduct foodsProduct = productService.selectProductById(product.getProId());
            if (null != foodsProduct) {
                product.setProName(foodsProduct.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出奖盒关联商品列表
     */
    @Log(title = "奖盒关联商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoxProduct boxProduct) {
        List<BoxProduct> list = boxProductService.selectBoxProductList(boxProduct);
        ExcelUtil<BoxProduct> util = new ExcelUtil<BoxProduct>(BoxProduct.class);
        util.exportExcel(response, list, "奖盒关联商品数据");
    }

    /**
     * 获取奖盒关联商品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(boxProductService.selectBoxProductById(id));
    }

    /**
     * 新增奖盒关联商品
     */
    @Log(title = "奖盒关联商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoxProductParam boxProduct) {
        User currentUser = TokenUtils.getCurrentUser();
        Date date = new Date();
        List<BoxProduct> boxProducts = new ArrayList<>();
        for (Long proId : boxProduct.getProIds()) {
            BoxProduct boxProductNew = new BoxProduct();
            boxProductNew.setBoxId(boxProduct.getBoxId());
            boxProductNew.setProId(proId);
            boxProductNew.setProType(boxProduct.getProType());
            boxProductNew.setCreatedBy(String.valueOf(currentUser.getId()));
            boxProductNew.setUpdatedBy(String.valueOf(currentUser.getId()));
            boxProductNew.setCreatedTime(date);
            boxProductNew.setUpdatedTime(date);
            boxProducts.add(boxProductNew);
        }

        return toAjax(boxProductService.saveBatch(boxProducts));
    }

    /**
     * 修改奖盒关联商品
     */
    @Log(title = "奖盒关联商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoxProduct boxProduct) {
        return toAjax(boxProductService.updateBoxProduct(boxProduct));
    }

    /**
     * 删除奖盒关联商品
     */
    @Log(title = "奖盒关联商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(boxProductService.deleteBoxProductByIds(ids));
    }


    /**
     * 魔力赏下单
     */
    @PostMapping(value = "/mlsAddOrder")
    public AjaxResult mlsAddOrder(@RequestBody  BoxAddOrderParam boxAddOrderParam) {
        return AjaxResult.success(boxProductService.mlsAddOrder(boxAddOrderParam));
    }


    /**
     * 一番赏下单
     */
    @PostMapping(value = "/yfsAddOrder")
    public AjaxResult yfsAddOrder(@RequestBody BoxAddOrderParam boxAddOrderParam) {
        return AjaxResult.success(boxProductService.yfsAddOrder(boxAddOrderParam));
    }


}
